package com.wangtianxiang.rabbitmq.config;


import org.springframework.util.Assert;

import java.time.Duration;

public class RabbitMqProperties {
    private String host;
    private String port;
    private String username;
    private String password;
    private String virtualHost;
    /**
     * Whether to enable publisher confirms.
     */
    private Boolean publisherConfirms;
    /**
     * Whether to enable publisher returns.
     */
    private Boolean publisherReturns;

    private String confirmCallback;

    private String returnCallback;
    /**
     * alias for datasource,use comma for multiple alias
     */
    private String dataSourceAlias;

    private final Template template = new Template();

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVirtualHost() {
        return virtualHost;
    }

    public void setVirtualHost(String virtualHost) {
        this.virtualHost = virtualHost;
    }

    public String getConfirmCallback() {
        return confirmCallback;
    }

    public void setConfirmCallback(String confirmCallback) {
        this.confirmCallback = confirmCallback;
    }

    public String getReturnCallback() {
        return returnCallback;
    }

    public void setReturnCallback(String returnCallback) {
        this.returnCallback = returnCallback;
    }

    public Template getTemplate() {
        return template;
    }


    public boolean getPublisherConfirms() {
        return publisherConfirms;
    }

    public void setPublisherConfirms(boolean publisherConfirms) {
        this.publisherConfirms = publisherConfirms;
    }

    public boolean getPublisherReturns() {
        return publisherReturns;
    }

    public void setPublisherReturns(boolean publisherReturns) {
        this.publisherReturns = publisherReturns;
    }

    public String getDataSourceAlias() {
        return dataSourceAlias;
    }

    public void setDataSourceAlias(String dataSourceAlias) {
        this.dataSourceAlias = dataSourceAlias;
    }

    public static class Template{

        private final Retry retry = new Retry();


        /**
         * Name of the default exchange to use for send operations.
         */
        private String exchange = "";

        /**
         * Value of a default routing key to use for send operations.
         */
        private String routingKey = "";

        private Boolean mandatory;

        public String getExchange() {
            return exchange;
        }

        public void setExchange(String exchange) {
            this.exchange = exchange;
        }

        public Boolean getMandatory() {
            return mandatory;
        }

        public void setMandatory(Boolean mandatory) {
            this.mandatory = mandatory;
        }

        public Retry getRetry() {
            return retry;
        }

        public String getRoutingKey() {
            return routingKey;
        }

        public void setRoutingKey(String routingKey) {
            this.routingKey = routingKey;
        }
    }

    public static class Retry {

        /**
         * Whether publishing retries are enabled.
         */
        private boolean enabled;

        /**
         * Maximum number of attempts to deliver a message.
         */
        private int maxAttempts = 3;

        /**
         * Duration between the first and second attempt to deliver a message.
         */
        private Duration initialInterval = Duration.ofMillis(1000);

        /**
         * Multiplier to apply to the previous retry interval.
         */
        private double multiplier = 1.0;

        /**
         * Maximum duration between attempts.
         */
        private Duration maxInterval = Duration.ofMillis(10000);

        public boolean isEnabled() {
            return this.enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getMaxAttempts() {
            return this.maxAttempts;
        }

        public void setMaxAttempts(int maxAttempts) {
            this.maxAttempts = maxAttempts;
        }

        public Duration getInitialInterval() {
            return this.initialInterval;
        }

        public void setInitialInterval(Duration initialInterval) {
            this.initialInterval = initialInterval;
        }

        public double getMultiplier() {
            return this.multiplier;
        }

        public void setMultiplier(double multiplier) {
            this.multiplier = multiplier;
        }

        public Duration getMaxInterval() {
            return this.maxInterval;
        }

        public void setMaxInterval(Duration maxInterval) {
            this.maxInterval = maxInterval;
        }

    }


    public void afterPropertiesSet() throws Exception {
        Assert.hasText(host, "host must not be empty");
        Assert.hasText(port, "port must not be empty");
        Assert.hasText(username, "username must not be empty");
        Assert.hasText(password, "password must not be empty");
        Assert.hasText(virtualHost, "virtualHost must not be empty");

    }


}
